﻿#region Using Statements
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
#endregion

namespace DirectZZT.Shared.Graphics
{
    /// <summary>
    /// Provides easy access to ZZT's color palette.
    /// </summary>
    public static class ZZTColor
    {
        private static Dictionary<byte, Color> ColorsCached;

        /// <summary>
        /// Build up ZZT color palette.
        /// </summary>
        static ZZTColor()
        {
            ColorsCached = new Dictionary<byte, Color>(16);
            // TODO: find out colors
            ColorsCached.Add(0x00, Color.Black);
            ColorsCached.Add(0x01, Color.DarkBlue);
            ColorsCached.Add(0x02, Color.DarkGreen);
            ColorsCached.Add(0x03, Color.DarkCyan);
            ColorsCached.Add(0x04, Color.DarkRed);
            ColorsCached.Add(0x05, Color.Purple);
            ColorsCached.Add(0x06, Color.Brown);
            ColorsCached.Add(0x07, Color.LightGray);
            ColorsCached.Add(0x08, Color.DarkGray);
            ColorsCached.Add(0x09, Color.LightBlue);
            ColorsCached.Add(0x0A, Color.LightGreen);
            ColorsCached.Add(0x0B, Color.LightCyan);
            ColorsCached.Add(0x0C, Color.Red);
            ColorsCached.Add(0x0D, Color.LightPink);
            ColorsCached.Add(0x0E, Color.LightYellow);
            ColorsCached.Add(0x0F, Color.White);
        }

        /// <summary>
        /// Returns the color from the ZZT color palette that has the given code.
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Color FromCode(byte code)
        {
            if (ColorsCached.ContainsKey(code))
            {
                return ColorsCached[code];
            }
            return Color.White;
        }

        /// <summary>
        /// Returns the color from the ZZT color palette that has the given code.
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Color FromCode(ColorCode code)
        {
            return FromCode((byte)code);
        }

        /// <summary>
        /// Returns the color code in the ZZT color palette from the given XNA color instance.
        /// Returns White if the color did not exist in the ZZT color palette.
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static byte ToCode(Color code)
        {
            var v = ColorsCached.Where(p => p.Value == code);
            if (v.Count() == 0)
            {
                return 0x00;
            }
            return v.First().Key;
        }

        /// <summary>
        /// Specifies the ZZT color palette.
        /// </summary>
        public enum ColorCode : byte
        {
            /// <summary>
            /// Black color.
            /// </summary>
            Black = 0x00,
            /// <summary>
            /// Dark Blue color.
            /// </summary>
            DarkBlue = 0x01,
            /// <summary>
            /// Dark Green color.
            /// </summary>
            DarkGreen = 0x02,
            /// <summary>
            /// Dark Cyan color.
            /// </summary>
            DarkCyan = 0x03,
            /// <summary>
            /// Dark Red color.
            /// </summary>
            DarkRed = 0x04,
            /// <summary>
            /// Purple color.
            /// </summary>
            Purple = 0x05,
            /// <summary>
            /// Brown color.
            /// </summary>
            Brown = 0x06,
            /// <summary>
            /// Light gray color.
            /// </summary>
            LightGray = 0x07,
            /// <summary>
            /// Dark gray color.
            /// </summary>
            DarkGray = 0x08,
            /// <summary>
            /// Light blue color.
            /// </summary>
            LightBlue = 0x09,
            /// <summary>
            /// Light green color.
            /// </summary>
            LightGreen = 0x0A,
            /// <summary>
            /// Light cyan color.
            /// </summary>
            LightCyan = 0x0B,
            /// <summary>
            /// Red color.
            /// </summary>
            Red = 0x0C,
            /// <summary>
            /// Light Pink color.
            /// </summary>
            LightPink = 0x0D,
            /// <summary>
            /// Light Yellow color.
            /// </summary>
            LightYellow = 0x0E,
            /// <summary>
            /// White color.
            /// </summary>
            White = 0x0F,
        }
    }
}
